//
//  HWChooseView.m
//  嗨文
//
//  Created by 响当当 on 5/8/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "HWChooseView.h"

@interface HWChooseView()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    NSInteger _yearIndex;
    
    NSInteger _monthIndex;
}
@property (nonatomic, strong)UIPickerView *timePickeView;//
@property (nonatomic, strong)UIView *topView;//

@property (nonatomic, copy) NSArray *yearArray;
@end

@implementation HWChooseView

- (NSArray *)yearArray {
    
    if (_yearArray == nil) {
        
        _yearArray = [[NSArray alloc]init];
    }
    
    return _yearArray;
}

- (UIPickerView *)timePickeView
{
if (!_timePickeView)
{
    _timePickeView = [[UIPickerView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT-150*kUIScaleBasePlus,SCREEN_WIDTH, 150*kUIScaleBasePlus)];
    _timePickeView.delegate = self;
    _timePickeView.dataSource =self;
    _timePickeView.backgroundColor = [UIColor whiteColor];
}
return _timePickeView;
}

- (UIView *)topView
{
    if (!_topView)
    {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0,SCREEN_HEIGHT-150*kUIScaleBasePlus-40*kUIScaleBasePlus, SCREEN_WIDTH, 40*kUIScaleBasePlus)];
        _topView.backgroundColor = RGBACOLOR(242, 242, 242, 1.0);
        
        UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        cancelBtn.frame = CGRectMake(0, 0,100*kUIScaleBasePlus,40*kUIScaleBasePlus);
        [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [cancelBtn setTitleColor:LCHexColor(0x000000) forState:UIControlStateNormal];
        [cancelBtn.titleLabel setFont:[UIFont systemFontOfSize:16*kUIScaleBasePlus]];
        [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:cancelBtn];
        
        UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        yesBtn.frame = CGRectMake(SCREEN_WIDTH - 100*kUIScaleBasePlus, 0, 100*kUIScaleBasePlus,40*kUIScaleBasePlus);
        [yesBtn setTitle:@"完成" forState:UIControlStateNormal];
        [yesBtn setTitleColor:LCHexColor(0xF54337) forState:UIControlStateNormal];
        [yesBtn.titleLabel setFont:[UIFont systemFontOfSize:16*kUIScaleBasePlus]];
        [yesBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:yesBtn];
    }
    return _topView;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.75];
        [self addSubview:self.topView];
        [self addSubview:self.timePickeView];
        [self.timePickeView selectRow:_yearIndex inComponent:0 animated:YES];
    }
    return self;
}

- (void)remove {
    
    [UIView animateWithDuration:0.25 animations:^{
        
        self.topView.frame = CGRectMake(0,SCREEN_HEIGHT-150*kUIScaleBasePlus-40*kUIScaleBasePlus, SCREEN_WIDTH, 40*kUIScaleBasePlus);
        self.timePickeView.frame = CGRectMake(0,SCREEN_HEIGHT-150*kUIScaleBasePlus,SCREEN_WIDTH, 150*kUIScaleBasePlus);
        
    } completion:^(BOOL finished) {
        
        [self removeFromSuperview];
    }];
    
}

#pragma mark - delegate & datasource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.yearArray.count;
}

#pragma mark -UIPickerView的代理

// 滚动UIPickerView就会调用
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _yearIndex = row;
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        UILabel *label = (UILabel *)[pickerView viewForRow:row forComponent:component];
        label.textColor = RGBACOLOR(26, 174, 135, 1.0);
        label.font = [UIFont systemFontOfSize:16*kUIScaleBasePlus];
        
    });
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    
    //设置文字的属性
    UILabel *genderLabel = [[UILabel alloc] init];
    genderLabel.textAlignment = NSTextAlignmentCenter;
    genderLabel.textColor = RGBACOLOR(153, 153, 153, 1.0);
    genderLabel.font = [UIFont systemFontOfSize:14*kUIScaleBasePlus];
    genderLabel.text = self.yearArray[row];
    
    
    
    return genderLabel;
}


#pragma mark - Method
- (void)cancelClick
{
    [self remove];
}

- (void)btnClick
{
    
    if (_yearIndex == 0)
    {
        if (self.getDateBlock)
        {
            NSString *timeStr = [NSString stringWithFormat:@"%@",((UILabel *)[self.timePickeView viewForRow:_yearIndex forComponent:0]).text];
            self.getDateBlock(timeStr);
            [self remove];
        }
    }
    else
    {
        if (self.getDateBlock)
        {
            NSString *timeStr = [NSString stringWithFormat:@"%@",((UILabel *)[self.timePickeView viewForRow:_yearIndex forComponent:0]).text];
            self.getDateBlock(timeStr);
            [self remove];
        }
    }
}

- (void)getDate:(NSArray *)arr
{
    self.yearArray = [NSArray arrayWithArray:arr];
    [self.timePickeView reloadAllComponents];
    [self.timePickeView selectRow:_yearIndex inComponent:0 animated:YES];
}
@end
